package com.simpletour.qa.common.entity;

import com.fasterxml.jackson.annotation.JsonBackReference;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import javax.persistence.*;
import java.io.Serializable;
import java.util.List;
import java.util.Set;

/**
 * 用户角色
 * @author zhaohw
 * @date 2018-06-19 17:43
 */
@Setter
@Getter
@Table(schema = "qa",name = "t_role")
@NoArgsConstructor
@Entity
public class Role implements Serializable {

    /**
     * 默认角色id
     */
    public static Integer DEFAULT_ROLE_ID = 2;

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    @Column(name = "role_id")
    private Integer roleId;

    private String name;

    @ManyToMany(cascade = CascadeType.PERSIST)
    @JoinTable(name = "qa.t_role_ref_authority",
            joinColumns = {@JoinColumn(name = "role_id", referencedColumnName = "role_id")},
            inverseJoinColumns = {@JoinColumn(name = "authority_id", referencedColumnName = "id")})
    private List<Authority> authorities;

    @JsonBackReference
    @ManyToMany(mappedBy = "roles")
    private Set<User> users;

    public Role(Integer roleId, String name) {
        this.roleId = roleId;
        this.name = name;
    }
}
